package GUI;

import gameEngine.PlayerHand;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class HoldemPanel extends JPanel{
	private int width, height;
	private final int H_MARGIN = 100, V_MARGIN = 50;
	private int table_x, table_y, table_w, table_h;
	private PokerTable players;
	private int turn;
	
	public HoldemPanel(ControlPane controlPane, int width, int height){
		this.players = controlPane.getPlayers();
		this.setSize(width, height);
		this.width = width - (2 * H_MARGIN);
		this.height = height - (2 * V_MARGIN);
		this.turn = 0;
		table_x = table_y = 0;
		table_h = table_w = 0;
	}
	public void paint(Graphics g){
		table_x = getX() + H_MARGIN;
		table_y = getY() + V_MARGIN;
		table_w = width - (2 * H_MARGIN);
		table_h = height - (2 * V_MARGIN);
		Image img;
		try {
			String imgPath = HoldemPanel.class.getResource("images/board.gif").getPath();
			img = ImageIO.read(new File(imgPath));
			g.drawImage(img, 0, 0, width, height, this);
		} catch (IOException e) {
			g.setColor(Color.YELLOW);
			g.fillRect(0, 0, width, height);
		}
		g.setColor(Color.GREEN);
		g.fillRoundRect(table_x, table_y, table_w , table_h, width / 5, height / 5);
		drawSpots(g);
	}
	public PokerTable getActivePlayers(){
		return players;
	}
	synchronized
	private void drawSpots(Graphics g){
		if(this.players.isEmpty())return;
		int num = players.size();
		int rad1 , rad2 , margin_x, margin_y;
		rad1 = table_w /  num;
		rad2 = table_h / num;
		margin_x = table_w / (2 * num);
		margin_y = (table_h / (2 * num)) + rad2 / num;
		int x_up = table_x + margin_x + ((num/2) * (3 * margin_x));
		int x_down = table_x + margin_x;
		int y_up = table_y + margin_y;
		int y_down = table_y + table_h - (2 * margin_y);
		for(int i = num-1; i>=0; i--){
			g.setColor(Color.BLACK);
			Player player = players.get(i);
			if(i > num/2){
				drawPlayer(g, player, rad1, rad2, x_up, y_up);
				x_up -= 3 * margin_x;
			}else{
				drawPlayer(g, player, rad1, rad2, x_down, y_down);
				x_down += 3 * margin_x;
			}
		}
		if(turn > 0){
			displayCards(g, players.nextFaceUp());
		}
		this.turn++;
	}
	private void displayCards(Graphics g, Card[] cards){
		int x = table_x + (table_w/2) - table_w/8;
		int y = table_y + (table_h/2) - table_h/8;
		int height = 70, width = 50;
		for(Card card:cards){
			try {
				g.drawImage(card.getImage(), x, y, width, height, this);
			} catch (IOException e) {
				g.draw3DRect(x, y, width, height, true);
			}
			x+=20;
			y+=20;
		}
	}
	private void drawPlayer(Graphics g, Player player, int rad1, int rad2, int x, int y){
		if(!player.isActive())return;
		int height = 70, width = 50;
		int X= x, Y = y;
		PlayerHand pHand = player.getPlayerHand();
		try {
			if(player.isOwner()){
				for(int i = 0; i < 2 ; i++){
					g.drawImage( pHand.getCard1().getImage(), X, Y, width, height, this);
					g.drawImage( pHand.getCard2().getImage(), X, Y, width, height, this);
					X += 20;
					Y += 20;
				}
			}else{
				for(int i = 0; i < 2 ; i++){
					g.drawImage( Card.faceDownCard(pHand.getCard1()).getImage(), X, Y, width, height, this);
					g.drawImage( Card.faceDownCard(pHand.getCard2()).getImage(), X, Y, width, height, this);	
					X += 20;
					Y += 20;
				}
			}
		} catch (IOException e) {
			g.fillOval(x, y, rad1, rad2);
		}
		g.setColor(Color.RED);
		g.drawString(player.getName(), x, y);
		g.drawString(player.getStatus(), x, y+rad2/2);
	}
}
